﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace Report.GetReportSQL
{
    public partial class frmMain : Form
    {
        private readonly string SQL_FORMAT =
            "把数据插入到你的报表SQL存储表中 insert into ......";

        #region 初始化
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            edtFileType.SelectedIndex = 0;
        }
        #endregion

        #region 获取文件清单信息
        /// <summary>
        /// 获得指定目录下的所有文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public List<FileInfo> GetFilesByDir(string path, string filter = "*.*")
        {
            if(string.IsNullOrEmpty(path) == true) return null;
            DirectoryInfo di = new DirectoryInfo(path);

            //找到该目录下的文件
            FileInfo[] fi = di.GetFiles(filter);

            //把FileInfo[]数组转换为List
            List<FileInfo> list = fi.ToList<FileInfo>();
            return list;
        }

        /// <summary>
        /// 获得指定目录及其子目录的所有文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public List<FileInfo> GetAllFilesByDir(string path, string filter = "*.*")
        {
            if (string.IsNullOrEmpty(path) == true) return null;
            DirectoryInfo dir = new DirectoryInfo(path);

            //找到该目录下的文件
            FileInfo[] fi = dir.GetFiles(filter);

            //把FileInfo[]数组转换为List
            List<FileInfo> list = fi.ToList<FileInfo>();

            //找到该目录下的所有目录里的文件
            DirectoryInfo[] subDir = dir.GetDirectories();
            foreach (DirectoryInfo d in subDir)
            {
                List<FileInfo> subList = GetFilesByDir(d.FullName, filter);
                foreach (FileInfo subFile in subList)
                {
                    list.Add(subFile);
                }
            }
            return list;
        }

        public List<FileInfo> GetAllFilesList(string path, string filter = "*.*")
        {
            if (string.IsNullOrEmpty(path) == true)
            {
                return null;
            }

            DirectoryInfo dir = new DirectoryInfo(path);

            //找到该目录下的文件
            FileInfo[] fi = dir.GetFiles(filter);

            //把FileInfo[]数组转换为List
            List<FileInfo> list = fi.ToList<FileInfo>();

            //找到该目录下的所有目录再递归
            DirectoryInfo[] subDir = dir.GetDirectories();
            foreach (DirectoryInfo d in subDir)
            {
                List<FileInfo> subList = GetAllFilesList(d.FullName, filter);
                foreach (FileInfo subFile in subList)
                {
                    list.Add(subFile);
                }
            }
            return list;
        }
        #endregion

        #region 文件清单获取测试
        private string testGetfile()
        {
            string filePath = edtPath.Text;
            StringBuilder sb = new StringBuilder();
            sb.Append("测试1,获得指定目录下的所有文件：\n");
            List<FileInfo> list1 = GetFilesByDir(filePath, edtFileType.Text);
            foreach (FileInfo fi in list1)
            {
                sb.Append(fi.FullName.ToString() + "\n");// + " 文件名：" + fi.Name);
            }

            sb.Append("\n\n测试2,获得指定目录及其子目录的所有文件：\n");
            List<FileInfo> list2 = GetAllFilesList(filePath, edtFileType.Text);

            foreach (FileInfo fi in list2)
            {
                //edtResponse.AppendText(fi.FullName.ToString() + "\n");// + " 文件名：" + fi.Name + "\n");
                sb.Append(fi.FullName.ToString() + "-" +
                    fi.DirectoryName + "-" +
                    fi.Name + "-" +
                    Path.GetFileNameWithoutExtension(fi.Name) + "-" +
                    "\n");
            }
            return sb.ToString();
        }
        #endregion

        private string GetSQL(params string[] args)
        {
            return string.Format(SQL_FORMAT, args);
        }
        private void PrintMsg(string sMsg)
        {
            edtMsg.AppendText(sMsg + "\n");
            edtMsg.ScrollToCaret();
        }
        private void ClearMsg()
        {
            edtMsg.Clear();
        }
        private Hashtable GetRptSQL(string rptfileName)
        {
            if (edtFileType.Text.ToUpper() == "*.XSD")
                return SchemaXmlHelper.getXSDfileSQL(rptfileName);
            else if (edtFileType.Text.ToUpper() == "*.JRXML")
                return SchemaXmlHelper.getJRXMLfileSQL(rptfileName);
            else
                return null;
        }
        private void btnGetSQL_Click(object sender, EventArgs e)
        {
            if (cbClearResult.Checked) ClearMsg();
            string stemp= string.Empty;
            string sDataSet = string.Empty;
            //System.IO.Path.GetFileNameWithoutExtension(filename);
            string sTableName = string.Empty;
            string sRptSQL = string.Empty;
            int iProgress = 0;

            StringBuilder sb = new StringBuilder();
            List<FileInfo> FileList = GetAllFilesList(edtPath.Text, edtFileType.Text);
            if ((FileList == null) || (FileList.Count == 0))
                return;

            ProgressBarSQL.Minimum = 1;
            ProgressBarSQL.Maximum = FileList.Count;
            iProgress = 1;
            foreach (FileInfo fi in FileList)
            {
                ProgressBarSQL.Value = iProgress;
                sDataSet = Path.GetFileNameWithoutExtension(fi.Name);

                System.Collections.Hashtable htSQL = GetRptSQL(fi.FullName.ToString());
                foreach (System.Collections.DictionaryEntry de in htSQL)
                {
                    sTableName = de.Key.ToString();
                    sRptSQL = de.Value.ToString();
                    PrintMsg("-- " + sDataSet + "." + sTableName + " --");
                    //创建数据库SQL插入语句
                    if (cbCreateSQL.Checked) sRptSQL = GetSQL(sDataSet, sTableName, sRptSQL);
                    PrintMsg(sRptSQL);
                }
                iProgress++;
            }
            GC.Collect();
        }

        private void btnPath_Click(object sender, EventArgs e)
        {
            if (fbDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                edtPath.Text = fbDialog.SelectedPath;
        }

    }
}
